package com.cat.model.biz.service.admin.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.service.admin.SolutionService;
import com.cat.model.common.model.common.ReservationVO;
import com.cat.model.common.model.common.SolutionModelVO;
import com.cat.model.common.model.common.SolutionVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.bean.ReservationBean;

@Service("SolutionService")
public class SolutionServiceImpl implements SolutionService{
	@Autowired
	private TxCommonDAO commonDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private PlmCommDAO plmCommDAO;

	@Override
	public SolutionVO createSolution(SolutionVO solution) {
		if(solution.getId() == null){
			solution.setLastUpdateTime(new Date());
			solution.setStatus("01");//01-待确认
			commonDAO.insert(solution);
		} else {
			commonDAO.updateById(solution, "totalAmount","modelNum", "workHours","workCityCode","workAddress","workType");
			List<SolutionModelVO> modelVOList=modelMapper.getSolutionModel(solution.getId());
			for (SolutionModelVO solutionModelVO : modelVOList) {
				solutionModelVO.setWorkCityCode(solution.getWorkCityCode());
				commonDAO.updateById(solutionModelVO, "workCityCode");
			}
		}
		
		ReservationVO reservation =modelMapper.getReservation(solution.getReservationId());
		if(reservation!=null){
			reservation.setStatus("02");//02-已受理
			reservation.setWorkCityCode(solution.getWorkCityCode());
			reservation.setTotalAmount(solution.getTotalAmount());
			commonDAO.updateById(reservation, "status","workCityCode","totalAmount");
		}
		
		return solution;
	}

	@Override
	public int deleteSolution(long id) {
		SolutionVO solution = new SolutionVO();
		solution.setId(id);
		
		List<SolutionModelVO> modelList = modelMapper.getSolutionModel(id);
		if(modelList!=null&&modelList.size()>0){
			for (int i = 0; i < modelList.size(); i++) {
				SolutionModelVO model0 = modelList.get(i);
				SolutionModelVO model = new SolutionModelVO();
				model.setId(model0.getId());
				commonDAO.deleteById(model);
			}
		}
		
		return commonDAO.deleteById(solution);
	}

	@Override
	public int updateSolution(SolutionVO solution) {
		return commonDAO.updateById(solution, "modelNum", "workHours","workCityCode","workAddress","workType","totalAmount");
	}

	@Override
	public SolutionModelVO createSolutionModel(SolutionModelVO solutionModel) {
		commonDAO.insert(solutionModel);
		
		return solutionModel;
	}

	@Override
	public int deleteSolutionModel(long id) {
		SolutionModelVO solution = new SolutionModelVO();
		solution.setId(id);
		return commonDAO.deleteById(solution);
	}

	@Override
	public int updateSolutionModel(SolutionModelVO solutionModel) {
		return commonDAO.updateById(solutionModel, "modelId", "workStartTime","workEndTime","workHours","unitAmount");
	}

	@Override
	public SolutionVO createInitSolution(long reservationId) {
		ReservationBean reservation = plmCommDAO.getReservationDetail(new Long(reservationId));
		SolutionVO solution = new SolutionVO();
		solution.setReservationId(new Long(reservationId));
		solution.setWorkAddress(reservation.getWorkAddress());
		solution.setWorkCityCode(reservation.getWorkCityCode());
		solution.setWorkHours(reservation.getWorkHours());
		solution.setWorkType(reservation.getWorkType());
		solution.setTotalAmount(reservation.getTotalAmount());
		solution.setStatus("01");//01-待确认
		solution.setLastUpdateTime(new Date());
		
		commonDAO.insert(solution);
		
		List<ReservationBean> shortList = plmCommDAO.getReservationShortlist(reservationId);
		if(shortList!=null&&shortList.size()>0){
			for (int i = 0; i < shortList.size(); i++) {
				ReservationBean model0 = shortList.get(i);
				SolutionModelVO model = new SolutionModelVO();
				model.setId(model0.getId());
				model.setSolutionId(solution.getId());
				model.setModelId(model0.getModelId());
				model.setWorkStartTime(model0.getWorkStartTime());
				model.setWorkEndTime(model0.getWorkEndTime());
				model.setWorkHours(model0.getWorkHours());
				model.setUnitAmount(model0.getUnitAmount());
				model.setWorkCityCode(reservation.getWorkCityCode());
				commonDAO.insert(model);
			}
		}
		
		return solution;
	}
	
	
}
